package com.scrum.delegates;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scrum.services.IUserServices;
import com.scrum.vos.DelegateConfirmation;
import com.scrum.vos.DelegateConfirmationState;
import com.scrum.vos.User;

@Service
@Transactional
public class ScrumDelegate implements IScrumDelegate{

	@Autowired 
	IUserServices userServices;
	
	@Override
	public DelegateConfirmation createUser(User user) {

		DelegateConfirmation delegateConfirmation = new DelegateConfirmation();
		
		try {
			
			userServices.createUser(user);
		
		//Cualquier error surgido en las capas inferiores es capturado aqui
		} catch (Exception e) {
			e.printStackTrace();
			delegateConfirmation.setDelegateConfirmationState(DelegateConfirmationState.ERROR);
			delegateConfirmation.setMessage(e.getMessage());
			return delegateConfirmation;
		}


		delegateConfirmation.setDelegateConfirmationState(DelegateConfirmationState.OK);
		return delegateConfirmation;
	}

	@Override
	public String login(String user, String password) {
		String sessionToken;
		
		try {
			sessionToken = userServices.login(user, password);
		
		//Cualquier error surgido en las capas inferiores es capturado aqui
		} catch (Exception e) {
			sessionToken = null;
			e.printStackTrace();
		} 
		
		return sessionToken;
	}

	@Override
	public DelegateConfirmation logout(String sessionToken) {
		
		DelegateConfirmation delegateConfirmation = new DelegateConfirmation();

		try {
			userServices.logout(sessionToken);
			
		//Cualquier error surgido en las capas inferiores es capturado aqui
		} catch (Exception e) {
			e.printStackTrace();
			delegateConfirmation.setDelegateConfirmationState(DelegateConfirmationState.ERROR);		
			delegateConfirmation.setMessage(e.getMessage());
			return delegateConfirmation;
		}			
		delegateConfirmation.setDelegateConfirmationState(DelegateConfirmationState.OK);		
		return delegateConfirmation;
	}

}
